#include "window/port.h"
#include "ugui_core.h"
#include "ugui_font.h"

#define EVENT_BTN_UP    (0x01)
#define EVENT_BTN_DOWN  (0x02)
#define EVENT_BTN_RIGHT (0x03)
#define EVENT_BTN_LEFT  (0x04)
#define EVENT_BTN_FUNC1 (0x05)
#define EVENT_BTN_FUNC2 (0x06)
#define EVENT_BTN_FUNC3 (0x07)
#define EVENT_BTN_FUNC4 (0x08)
#define EVENT_BTN_FUNC5 (0x09)
#define EVENT_BTN_FUNC6 (0x0a)

void set_pixel(int16_t x, int16_t y, UG_COLOR c) {
  frame_buffer[y][x][0] = c & 0xff;
  frame_buffer[y][x][1] = c >> 8;
}

static void init_handle() {}

static void btn_clicked_handle(uint32_t btn_id) {
  switch (btn_id) {
    case EVENT_BTN_UP:
      UG_DrawLine(5, 5, 50, 50, C_RED);
      UG_DrawRoundFrame(10, 20, 30, 50, 5, C_BLACK);
      UG_DrawCircle(60, 30, 10, C_BLACK);
      UG_DrawFrame(70, 20, 90, 40, C_GREEN);
      UG_FillFrame(40, 60, 15, 25, C_BLACK);
      break;
    case EVENT_BTN_DOWN:
      UG_PutString(10, 10, "hello");
      break;
    case 3:
      break;

    default:
      break;
  }
}

void color_init() {
  for (uint16_t r = 0; r < SCREEN_BUFFER_HEIGHT; r++) {
    for (uint16_t c = 0; c < SCREEN_BUFFER_WIDTH; c++) {
      frame_buffer[r][c][0] = 0xff;
      frame_buffer[r][c][1] = 0xff;
    }
  }
}

static UG_GUI gui;
void ui_demo_init(void) {
  color_init();
  UG_Init(&gui, set_pixel, 256, 128);
  UG_SetBackcolor(C_WHITE);
  UG_SetForecolor(C_BLACK);
  UG_FontSelect(&FONT_5X8);

  port_set_btn_handle(btn_clicked_handle);
  port_set_init_handle(init_handle);
}
